/// <reference path="jquery.min.js" />
/// <reference path="rsseasy.min.js" />
/// <reference path="iscroll.min.js" />
/// <reference path="md5.min.js" />
/// <reference path="../data/arealist.js" />
/// <reference path="webviewadapter.min.js" />
/// <reference path="../data/dictdata.js" />
/// <reference path="validated.app.js" />
/// <reference path="dialogbox.js" />

//APP配置基本信息
var RssApp = { "WwwHost": "", "ApiHost": "", "UpHost": "", "TimeUrl": "" }
RssApp["win"] = $(window);
RssApp["AllPage"] = $("section");  //获取所有页
RssApp["CurPage"] = RssApp["AllPage"].first().show();  //默认第一页
RssApp["Menu"] = $("footer>a");   //主菜单
RssApp["WinWidth"] = RssApp.win.width();  //APP窗口宽度
RssApp["WinHeight"] = RssApp.win.height();  //APP窗口高度
RssApp["OrderFreeUrl"] = "";   //免费订单接口地址定义
RssApp["OrderUrl"] = "";  //订单生成接口定义
RssApp["SmsCode"] = "";  //发送的短信验证码
RssApp["ReadyData"] = {};  //当APP启动
RssApp["LocationData"] = { lat: 40.057754, lng: 116.330123, accuracy: 35.0, address: "北京市昌平区建材城西路靠近如家快捷酒店(北京西三旗育新地铁站店)", province: "北京市", city: "北京市", district: "昌平区", street: "建材城西路", streetnum: "87号院", citycode: "010", adcode: 110114, buildingid: "", floor: "", speed: 0.0, once: true, time: 2000, poiname: "如家快捷酒店(北京西三旗育新地铁站店)", aoiname: "蓝调商务中心" };  //用户定位数据

RssApp.PageWidth = 640;
RssApp.win.resize(function () {
    RssApp.WinWidth = RssApp.win.width();
    $("html").css("font-size", parseInt((RssApp.WinWidth / RssApp.PageWidth) * 100));
}).resize();

RssApp["Menu"].click(function () {  //所有菜单
    RssApp["Menu"].removeClass();
    $(this).addClass("active");
    JsAdapter.ClearHistory().Submit();
});

//所有单页
var RssPage = {};
RssPage["MaskLayer"] = $("#masklayer");
RssPage.Append = function (pageid) {
    this.page = $(pageid);
    this.header = this.page.find(">header");
    this.title = this.header.find(">h1");
    this.left = this.header.find(".left");
    this.right = this.header.find(".right");
    this.article = this.page.find(">article");
    this.footer = this.page.find(">footer");
}
$("section").each(function () {
    var id = this.id;
    if (id) {
        RssPage[id] = new RssPage.Append("#" + id);
    }
});

//视图
var RssView = {}
RssView.add = function (name, layout) {
    if (typeof layout == "function") {
        RssView[name] = layout;
    }
}

//用户操作对象
var RssUser = {};
RssUser.Data = { "myid": 0 }   //用户数据
RssUser.SaveLogin = false;    //是否记住用户登录表单数据
RssUser.Login = {
    "account": "",
    "pwd": "",
    "save": function (account, pwd) {
        if (RssUser.SaveLogin) {
            Storage.Set("logindata", Object.toJson({ "account": account, "pwd": pwd }));
        }
        else {
            Storage.Set("logindata", "{}");
        }
    },
    "get": function () {
        var json = Storage.getJson("logindata");
        this.account = json["account"]||"";
        this.pwd = json["pwd"] || "";
    }
};
RssUser.IsLogin = function (ev) {
    if (!RssUser.Data["myid"]) {
        ev.preventDefault();
        location.hash = "#loginpage";
        return false;
    }
    return true;
}
RssUser.onLogin = function () {
    location.hash = RssUser.History;
}
RssUser.History = "#homepage";
RssUser.Update = function (json) {
    RssUser.Data = Storage.getJson("userdata");
    $.each(json, function (k, v) {
     RssUser.Data[k] = v;
    });
    Storage.Set("userdata", Object.toJson(RssUser.Data));
    this.onUpdate();
    
}
RssUser.onUpdate = function () { }
RssUser.LoginOut = function () {
    Storage.remove("userdata");
    RssUser.Update();

}
RssUser.Update();

//验证登录
$("[islogin]").click(function (ev) {
    RssUser.History = $(this).attr("islogin") || "/";
    RssUser.IsLogin(ev);
});

$(".hisback").click(function (ev) {
    ev.preventDefault();
});

//单页切换
window.onhashchange = function () {
    RssApp["AllPage"].hide();
    var hash = location.hash;
    $("video").each(function () {
        this.pause();
    });
    if (hash) {
        RssApp["Menu"].filter("[href='" + hash + "']").click();
        $(hash).show().trigger("load");
        return;
    }
    JsAdapter.ClearHistory({}).Submit();
    RssApp["AllPage"].first().show().trigger("load");
}
window.onhashchange();

/*页面滑动*/

$("#guidepage").on("pageslideend", function (ev, json) {
    if (json["curpage"] == json["totalpage"]) {
        $(this).hide();
    }
})

if (!Storage.Get("launch")) {
    Storage.Set("launch", "1");
    $("#guidepage").show();
}

JsAdapter.onRollBack = function (json) {
    history.back();
}


//订单适配器
function OrderHelper() {
    this.dict = {
        "mingcheng": "",  //商品名称
        "shopdesc": "",  //商品描述
        "fee": 0,   //支付费用
        "fenleiid": 0,  //支付分类
        "paytypeid": 1,  //支付类型
        "json": "",  //扩展参数
        "amount": 1   //数量
    };
    $.extend(OrderHelper.prototype, this.dict);
}
OrderHelper.prototype = new RssDict();
OrderHelper.prototype.validate = function () {
    try {
        if (!this.mingcheng || !this.mingcheng.match(/^.{1,20}$/)) {
            throw "订单商品名称不能为空,1~20个字符！";
        }
        if (isNaN(this.amount) || parseInt(this.amount) <= 0) {
            throw "订单商品数量必须大于0";
        }
        if (isNaN(this.fee) || parseFloat(this.fee) < 0) {
            throw "商品支付金额必须大于等于0";
        }
        if (isNaN(this.fenleiid) || parseFloat(this.feefenleiid) < 0) {
            throw "支付类别出错";
        }

        this.dict.mingcheng = this.mingcheng;
        this.dict.shopdesc = this.shopdesc;
        this.dict.fee = this.fee;
        this.dict.fenleiid = this.fenleiid;
        this.dict.paytypeid = this.paytypeid;
        this.dict.json = this.json;
        this.dict.amount = this.amount;

        return true;
    }
    catch (ex) {
        alert(ex);
    }
    return false;
}
OrderHelper.prototype.submit = function () {
    if (this.validate()) {
        var t = this;

        if (this.fee == 0) {
            new Ajax(RssApp.OrderFreeUrl).keyvalue(this.dict).getJson(function (data) {
                t.onsubmit(data["id"]);
            });
            return;
        }

        JsAdapter.Progress({ "style": true, "message": "正在生成订单" }).Submit();

        new Ajax(RssApp.OrderUrl).keyvalue(this.dict).getJson(function (data) {
            t.onsubmit(data["id"]);
        });
    }
    return this;
}
OrderHelper.prototype.onsubmit = function (orderid) {
}
OrderHelper.prototype.pay = function (orderid) {
    JsAdapter.Progress({ "style": true, "message": "准备支付" }).Submit();
    JsAdapter.WebView({ "url": "http://www.rsseasy.com/pay/mpay.aspx?" + Object.toQueryString($.extend({ "appid": 9, "orderid": orderid }, this.dict)) }).Submit();
}
OrderHelper.prototype.onpay = function () {

}

JsAdapter.onReady = function (json) {
    RssApp["ReadyData"] = json;
    Ajax.prototype.prikey = json["prikey"];
}
JsAdapter.Ready({}).Submit();

$("[contenteditable]").keydown(function (ev) {
    if (ev.keyCode == 9) {
        ev.preventDefault();
    }
});